package com.example.demo.refuelmanage.service.impl;

import com.example.demo.refuelmanage.mapper.RefuelMapper;
import com.example.demo.refuelmanage.service.RefuelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class RefuelServiceImpl implements RefuelService {
    @Autowired
    private RefuelMapper refuelMapper;


    /**
         * @Description 新增加油记录
         * @Author zhangxue
         * @Date 11:18 2018/7/12
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveRefuel(Map<String, Object> param) {
        int i = refuelMapper.saveRefuelMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 编辑加油记录
     * @Author zhangxue
     * @Date 11:18 2018/7/12
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editRefuel(Map<String, Object> param) {
        int i = refuelMapper.editRefuelMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
     * @Description 删除加油记录
     * @Author zhangxue
     * @Date 11:18 2018/7/12
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delRefuel(Map<String, Object> param) {
        int i = refuelMapper.delRefuelMapper(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(i==1){
            resultMap.put("status","1");
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("status","0");
        }
        return resultMap;
    }

    /**
         * @Description 查询所有加油记录
         * @Author zhangxue
         * @Date 11:00 2018/11/13
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> queryAllRefuel(Map<String, Object> param) {
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        resultList = refuelMapper.queryAllRefuel(param);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("data",resultList);
        return resultMap;

    }



}
